package Holmanordering;

import java.util.ArrayList;
import java.util.Collections;

public class Holman {
    public static void main(String[] args) {
        int[] arr={3,5,7,6,29,4};
        Node hol=holman(arr);
        hol.Preorder(hol);


    }
    public static Node holman(int[] arr) {
        ArrayList<Node> list = new ArrayList<>();
        for (int values : arr) {
            list.add(new Node(values));
        }

        while (list.size() > 1) {
            Collections.sort(list);
            Node left = list.get(0);
            Node right = list.get(1);
            Node values = new Node(left.values + right.values);
            values.left = left;
            values.right = right;
            list.remove(0);
            list.remove(1);
            list.add(values);
        }
            return list.get(0);
    }
       public void ProOrder(Node node){
        if (node!=null){
            node.Preorder(node);
        }else {
            System.out.println("null");
        }
       }
}
class Node implements Comparable<Node>{
    public int values;
    Node left;
    Node right;

    public Node(int values) {
        this.values = values;
    }

    @Override
    public String toString() {
        return "Node{" +
                "values=" + values +
                '}';
    }

    @Override
    public int compareTo(Node o) {
        return this.values-o.values;
    }

    public void Preorder(Node node){
        System.out.println(this);
        if (this.left!=null){
            this.left.Preorder(node);
        }
        if (this.right!=null){
            this.right.Preorder(node);
        }
    }

}